--[[ Copyright 2012, Chris Ho                               ]]--
--[[ MIT License (Please read the LICENSE file for details) ]]--

Use("common/Const");
Use("common/Log");
Use("common/Task");
Use("common/Pet");
Use("common/Skill");
Use("tasks/Charge");

-- I am a Tier 1 Archer.
local name = "A01";
local Initialize, status = loadfile(PERSONALITY_ARCHER);
Info(name .. ": trying archer personality");
if (Initialize == nil) then
  Info(name .. ": trying default personality (" .. status .. ")");
  Initialize, status = loadfile(PERSONALITY_DEFAULT);
  if (Initialize == nil) then
    Alert(name .. ": " .. status);
    return ME;
  end
end

-- Initialize base personality.
--local instance = Initialize(...);
--local instance = Initialize(arg[1]);
local instance = Initialize();



--[[ Select an attack for this target ]]--
function instance:Attack(_target)
  -- Calculate overall danger level.
  -- TODO: consider allies as well?
  local danger = self:Assess(ACTORS) + self.owner:Assess(ACTORS);
  local sp = GetV(V_SP, self.id);

  -- Use a combat skill if the situation is dangerous.
  local skill = Skill.None(self.id);

  -- Adjust danger by tranquility.
  danger = danger / self:GetTranquility();

  -- MINIMUM SP: 12 for Double Strafe
  if (sp >= 12) then
    if (sp > (self.maxsp * (1.0 - danger))) then
      skill = Skill.Create(self.id, SKILL_DOUBLESTRAFE, 2);
    end
  end

  if (skill.code ~= 0) then
    Info(name .. ": Chose skill " .. skill.code .. " level " .. skill.level .. " for " .. _target .. " (danger=" .. danger .. ")");
  end

  self.target = _target;
  self.task = Charge.Create(self, skill, self.target);
end

Info(name .. " Loaded");
return instance;
